import { Component, OnInit, Output, EventEmitter } from "@angular/core";
import { UserService } from "./../user.service";
import "./wxLogin.js";
import { setTimeout } from "timers";

interface WechatType {
  appid: string;
  redirect_uri: string;
  state: string;
  scope: string;
}

@Component({
  selector: "wechat",
  templateUrl: "wechat.component.html",
  styleUrls: ["wechat.component.less"]
})
export class WechatComponent implements OnInit {
  @Output() setModal = new EventEmitter();
  wx_login_config: WechatType;
  constructor(private userService: UserService) {
    if (!this.wx_login_config) {
      this.userService.getWechat().subscribe(data => {
        if (data && data.code === 0 ) {
          this.wx_login_config = data.data;
          this._showWxLogin();
        } else {
          console.log("get wechat err" + data);
        }
      });
    } else {
      this._showWxLogin();
    }
  }

  ngOnInit() { }

  changeModal(action: string) {
    this.setModal.emit(action);
  }

  private _showWxLogin() {
    console.log(this.wx_login_config);
    var obj = new WxLogin({
      id:"wx_login_frame", 
      appid: this.wx_login_config.appid, 
      scope: this.wx_login_config.scope, 
      redirect_uri: this.wx_login_config.redirect_uri,
      state: this.wx_login_config.state
    });
  }
}